var requireWithoutCache = require('require-without-cache');
var url = require('url');
var resourceProperty = requireWithoutCache('./common.js', require);
var logger = require('tracer').colorConsole();
var util = require('../miscellaneous/util.js');

function requireProxy(module) {
  try {
    return requireWithoutCache(module, require);
  } catch (e) {
    logger.error(e)
  }
}

const flag = false;

module.exports = {
  'tenant/api/v1/user/details/view': function(req, res) {
    if (flag) {
      passThrough(req, res, requireProxy('../data/user.json'));
    } else {
      util.outJson(req, res, requireProxy('../data/user.json'));
    }
  },
  '/tenant/api/v1': function(req, res) {
    var pathname = url.parse(req.url).pathname;
    if (flag) {
      passThrough(req, res, requireProxy('../data' + pathname + '.json'));
    } else {
      util.outJson(req, res, requireProxy('../data' + pathname + '.json'));
    }
  },
  '/notify/api/v1/imsgs/unread': function(req, res) {
    if (flag) {
      passThrough(req, res, requireProxy('../data/count.json'));
    } else {
      util.outJson(req, res, requireProxy('../data/count.json'));
    }
  },
  '/notify/api/v1/imsgs/received': function(req, res) {
    if (flag) {
      passThrough(req, res, requireProxy('../data/received.json'));
    } else {
      util.outJson(req, res, requireProxy('../data/received.json'));
    }
  },
  '/notify/api/v1/imsgs/unknow': function(req, res) {
    if (flag) {
      passThrough(req, res, requireProxy('../data/product.json'));
    } else {
      util.outJson(req, res, requireProxy('../data/product.json'));
    }
  },

  '/api/example/': function(req, res) {
    passThrough(req, res, { errCode: 200 });
  },
  // '/api/v2/ticket/receivTicket': function(req, res) {
  //   util.proxy(req, res, 'http://10.1.200.7:7804'); //yexiang
  // },
  '/api/v2/file/upload': function(req, res) {
    //util.proxyMultiPartForm(req, res, 'http://10.1.200.3:7880'); //fb
    util.proxyMultiPartForm(req, res, 'http://10.1.1.34:7880'); //dongff
    // util.outJson(req, res, {errCode: 200}); //yexiang

  },
  '/api/': function(req, res) {
    var pathname = url.parse(req.url).pathname;
    try {
      passThrough(req, res, requireProxy('../data' + pathname + '.json'));
    } catch (e) {
      logger.error(e);
      util.outJson(req, res, { errCode: 404, message: e.message });
    }
  },
  '/jira/': function(req, res) {
    var pathname = url.parse(req.url).pathname;
    try {
      passThrough(req, res, requireProxy('../data' + pathname + '.json'));
    } catch (e) {
      logger.error(e);
      util.outJson(req, res, { errCode: 404, message: e.message });
    }
  },
  '/app/': function(req, res) {
    var pathname = url.parse(req.url).pathname;
    try {
      passThrough(req, res, requireProxy('../data' + pathname + '.json'));
    } catch (e) {
      logger.error(e);
      util.outJson(req, res, { errCode: 404, message: e.message });
    }
  }
}

//////////
//从这里保留 //
//////////

module.exports['/api/example/upload'] = function(req, res) {
  //outJson就直接返回
  util.formParse(req, function(err, fields, files) {
    if (err) {
      logger.error(err);
      util.outJson(req, res, { errCode: 201, message: err.message });
      return;
    }
    for (var param in fields) {
      logger.log(param + ': ' + fields[param]);
    }
    for (var param in files) {
      logger.log(param + ': ' + files[param].name, files[param].size, files[param].path);
    }
  })
  util.outJson(req, res, { year: 1994 });
}

module.exports['/api/example/outJson'] = function(req, res) {
  //outJson就直接返回
  util.outJson(req, res, {
    errCode: 200,
    message: '',
    data: [{
      id: 0,
      name: 'Kate',
      email: 'kate@gamil.com'
    }, {
      id: 1,
      name: 'Lily',
      email: 'lily@hotmail.com'
    }]
  });
}

module.exports['/api/example/passThrough'] = function(req, res) {
  //使用passThrough可以灵活配置，只要在passThrough改一个if语句就可以全部透传了
  passThrough(req, res, {
    "msg": "获取JS-API Signature成功",
    "code": "200",
    "signature": "eac1f9f124a7f765da3c3d5f08a566427198caee",
    "appid": "wx0f0bc4c285e789db",
    "noncestr": "cKk4k3tZZZ4O3fg0",
    "timestamp": 1456127406
  });
}

function passThrough(req, res, o, mark) {
  if (0) {
    util.paramLog(req)
    setTimeout(function() {
      util.outJson(req, res, o);
    }, 1);
    return;
  }
  util.proxy(req, res, resourceProperty.getUrl());
}

//////////
//到这里结束 //
//////////
